package cn.yitun.chat.yitunserverimcore.general.common.service.room.impl;

import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunMapUtil;
import cn.yitun.chat.yitunservercommonlib.converter.ot.RoomChat2ContentConverter;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.message.chat.Content;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.message.chat.Item;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.message.chat.Section;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.room.RoomChatContentDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.room.RoomChatLogDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.system.user.UserDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.room.RoomChatContentVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.room.RoomChatItemVo;
import cn.yitun.chat.yitunserverimbase.dao.room.RoomChatContentDao;
import cn.yitun.chat.yitunserverimbase.dao.room.RoomChatItemDao;
import cn.yitun.chat.yitunserverimcore.general.common.service.room.RoomChatLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: yitun
 * @date: 2016年8月23日 上午11:20:14
 */
@Service
public class RoomChatLogServiceImpl implements RoomChatLogService {
	@Resource
	private RoomChatContentDao roomChatDAO;
	@Resource
	private RoomChatItemDao roomChatItemDao;

	@Override
	public ResultMessage queryRoomChatContentList(Long roomId, RoomChatContentDto chatQuery) {

		RoomChatLogDto roomChatLogQuery = new RoomChatLogDto();
		roomChatLogQuery.setRoomId(roomId);
		roomChatLogQuery.setText(chatQuery.getText());
		roomChatLogQuery.setBegin(chatQuery.getBegin());
		roomChatLogQuery.setEnd(chatQuery.getEnd());

		boolean hasRoomId = (null == roomId || "".equals(roomId));
		PageInfo<Map<Long, Object>> contents = hasRoomId ? new PageInfo<>() : queryRoomChatContentList(roomChatLogQuery);


		ResultMessage m = new ResultMessage();
		m.put("roomId", roomId);
		m.put("contents", contents);
		return m;
	}
	@Override
	public PageInfo queryRoomChatContentList(RoomChatLogDto roomChatLogQuery) {

		List<Map<String, Object>> contents = new ArrayList<>();
		PageHelper.startPage(roomChatLogQuery.getCurrentPageNo(), roomChatLogQuery.getPageSize());
		List<RoomChatContentVo> chatContentList = roomChatDAO.listVO(YitunMapUtil.java2Map(RoomChat2ContentConverter.INSTANCE.dto2content(roomChatLogQuery),false)  );
		List<Long> contentIds = new ArrayList<>();
		for (RoomChatContentVo rcc : chatContentList) {
			contentIds.add(rcc.getId());
		}
		Map<Long, List<RoomChatItemVo>> chatItemMap = new HashMap<>();
		if (!contentIds.isEmpty()) {
			List<RoomChatItemVo> chatItemList = roomChatItemDao.getRoomChatItemListByContentIds(contentIds);
			for (RoomChatItemVo rci : chatItemList) {
				List<RoomChatItemVo> list = chatItemMap.get(rci.getMessageId());
				if (null == list) {
					list = new ArrayList<>();
					chatItemMap.put(rci.getRoomChatContentId(), list);
				}
				list.add(rci);
			}
		}

		for (RoomChatContentVo rcc : chatContentList) {

			Map<String, Object> map = new HashMap<String, Object>();
			Content content = new Content();
			UserDto userData = new UserDto();

			userData.setHead(rcc.getUserHead());
			userData.setId(rcc.getUserId());
			userData.setName(rcc.getUserName());
			userData.setNickname(rcc.getUserNickname());
			//userData.setRole(rcc.getUserRole());
			List<RoomChatItemVo> list = chatItemMap.get(rcc.getId());
			List<Section> sections = new ArrayList<Section>();
			if (null != list && !list.isEmpty()) {
				int index = -1;
				Section section;
				List<Item> items = null;
				for (RoomChatItemVo rci : list) {
					if (index != rci.getSection()) {
						index = rci.getSection();

						items = new ArrayList<Item>();
						section = new Section();
						section.setItems(items);
						sections.add(section);
					}

					Item item = new Item();
					item.setType(rci.getType());
					item.setValue(rci.getFilterValue());
					if (null != items) {
						items.add(item);
					}
				}
				content.setSections(sections);
				map.put("userData", userData);
				map.put("content", content);
				contents.add(0, map);
			}
		}
		return new PageInfo<>(contents) ;
	}
}
